﻿#region License

// Another free, open solution by folks who get it.
// Copyright © 2011 Business in Unison, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;

using FizzWare.NBuilder;

using FluentAssertions;

using Patterns.Cache;
using Patterns.Collections;

using TechTalk.SpecFlow;

namespace Patterns.Specifications.Cache
{
	[Binding]
	public class CachingSteps
	{
		private const string _cacheKey = "key";

		public static CacheEventType CurrentEventType
		{
			get { return (CacheEventType) ScenarioContext.Current["eventType"]; }
			set { ScenarioContext.Current["eventType"] = value; }
		}

		public static ICache CurrentCache
		{
			get { return ScenarioContext.Current.GetValue("cache") as ICache; } 
			set { ScenarioContext.Current["cache"] = value; }
		}

		[Given(@"I have created a new complex test object")]
		public void CreateComplexObject()
		{
			GeneralSteps.CurrentTestObject = Builder<ComplexTestObject>.CreateNew().Build();
		}

		[When(@"I cache the object")]
		public void CacheObject()
		{
			ICache cache = new MemoryCache();
			cache[_cacheKey] = GeneralSteps.CurrentTestObject;
			CurrentCache = cache;
		}

		[When(@"I retrieve the object")]
		public void RetrieveObject()
		{
			GeneralSteps.CurrentResult = CurrentCache[_cacheKey];
		}

		[When(@"I retrieve the non-null object")]
		public void RetrieveNonNullObject()
		{
			RetrieveObject();
			GeneralSteps.CurrentResult.Should().NotBeNull();
		}

		[When(@"I cache the object using a sliding expiration of (\d+) millisecond(?:s)?")]
		public void CacheObjectWithSlidingExpiration(int milliseconds)
		{
			ICache cache = new MemoryCache();
			cache.Set(_cacheKey, () => GeneralSteps.CurrentTestObject, slidingExpiration: TimeSpan.FromMilliseconds(milliseconds));
			CurrentCache = cache;
		}

		[When(@"I add a notification listener for the item")]
		public void AddListener()
		{
			CurrentCache.OnUpdate(_cacheKey, cacheEvent => CurrentEventType = cacheEvent.EventType);
		}


		[Then(@"the original object and the cached object should be the same")]
		public void AssertObjectsAreSame()
		{
			GeneralSteps.CurrentResult.Should().BeSameAs(GeneralSteps.CurrentTestObject);
		}

		[Then(@"I should receive a notification that an item has been removed")]
		public void AssertRemoveNotificationReceived()
		{
			CurrentEventType.Should().Be(CacheEventType.Remove);
		}
	}
}